package com.gitee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.bo.CommentBo;
import com.gitee.enums.OrderStatusEnum;
import com.gitee.enums.ResponseEnum;
import com.gitee.exception.CommentException;
import com.gitee.mapper.CommentMapper;
import com.gitee.pojo.Comment;
import com.gitee.service.ICommentService;
import com.gitee.service.IOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private IOrderService orderService;

    /**
     * 添加评价
     *
     * @param userId
     * @param commentBo
     * @return
     */
    @Transactional
    @Override
    public Boolean addComment(Integer userId, CommentBo commentBo) {
        LambdaQueryWrapper<Comment> queryWrapper = new QueryWrapper<Comment>().lambda().eq(Comment::getOrderId, commentBo.getOrderId());
        if (super.count(queryWrapper) > 0) {
            throw new CommentException(ResponseEnum.EXIST_COMMENT);
        }
        List<Comment> comments = commentBo.getCommentItems().stream().map(commentItem -> {
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentItem, comment);
            comment.setUserId(userId);
            comment.setOrderId(commentBo.getOrderId());
            return comment;
        }).collect(Collectors.toList());
        return super.saveBatch(comments) && orderService.updateOrderStatus(commentBo.getOrderId(), OrderStatusEnum.COMMENTED);
    }

    /**
     * 更新评价
     *
     * @param userId
     * @param commentBo
     * @return
     */
    @Transactional
    @Override
    public Boolean updateComment(Integer userId, CommentBo commentBo) {
        LambdaUpdateWrapper<Comment> updateWrapper = new UpdateWrapper<Comment>().lambda().eq(Comment::getOrderId, commentBo.getOrderId());
        super.remove(updateWrapper);
        List<Comment> comments = commentBo.getCommentItems().stream().map(commentItem -> {
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentItem, comment);
            comment.setUserId(userId);
            comment.setOrderId(commentBo.getOrderId());
            return comment;
        }).collect(Collectors.toList());
        return super.saveBatch(comments);
    }

    /**
     * 删除评价
     *
     * @param userId
     * @param orderId
     * @return
     */
    @Transactional
    @Override
    public Boolean deleteComment(Integer userId, Integer orderId) {
        LambdaUpdateWrapper<Comment> updateWrapper = new UpdateWrapper<Comment>().lambda().eq(Comment::getOrderId, orderId);
        return super.remove(updateWrapper) && orderService.updateOrderStatus(orderId, OrderStatusEnum.COMMITTED);
    }
}
